package jdbc.test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

/**
 * @author oldliu
 * @since 1.0
 */
public class UpdateStudent {
    public static void main(String[] args) {
        try {
            Class.forName("com.mysql.jdbc.Driver");//static{}
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return;
        }

        System.out.println("根据学号修改name和wechat号：");
        Scanner in=new Scanner(System.in);
        System.out.println("请输入学号：");
        long stuno=in.nextLong();
        System.out.println("请输入姓名：");
        in.nextLine();//回车不要
        String newName=in.nextLine();
        //newName=newName.replace("'","");
        //newName=newName.replace("#","");
        System.out.println("请输入微信号：");
        String wechat=in.nextLine();

        String url = "jdbc:mysql://localhost:3306/java79school?useSSL=false&characterEncoding=UTF-8&serverTimezone=GMT%2b8";
        Connection con = null;
        Statement st = null;
        try {
            con = DriverManager.getConnection(url, "root", "root1234");
            st = con.createStatement();
            String sql = "update s_student set sname='"+newName+"',wechat='"+wechat+"' where stuno="+stuno;
            System.out.println(sql);

            int i = st.executeUpdate(sql);
            System.out.printf("成功了修改%d行数据", i);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (st != null)
                    st.close();

            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                if (con != null)
                    con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
